// 商品信息
var tblShiporderSkuInfo;

// 页面初始化
$(function () {
    
    //默认基本信息显示
    showShiporderSkuInfoTab();
    
    // tab切换事件
    $('a[data-toggle="tab"]').on('show.bs.tab', function (e) {
        
        switch ($(this).attr("id")) {
        case "shiporderInfo":    //基本信息
            showShiporderInfoTab();
            break;
        case "shiporderSkuInfo":    //商品信息
        	showShiporderSkuInfoTab();
            break;
        case "noteInfo":    //内部标签
            showNoteInfo();
            break;             
        default:
            break;
        }
    });
    
});

// 单价，金额格式化
var numberParser = function(data){
    var returnValue = "-";
    var numberData = parseFloat(data);
    if(numberData===+numberData){
        returnValue = numberData.toFixed(2);
    }
    return returnValue;
};

// 基本信息
function showShiporderInfoTab() {
    var shiporderId = $('#shiporderId', window.parent.document).val();

    $.ajax({
        type: "GET",
        url: "/shiporder/getShiporderInfo/" + shiporderId + ".action",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        async: false,//同步
        success: function (data) {
            if (data.result == "success") {
                $("#storName").text(data.data.storName);
                $("#lgstName").text(data.data.lgstName); 
                $("#lgstNo").text(data.data.lgstNo); 
                if (data.data.shipTime != null) {
                    $("#shipTime").text(data.data.shipTime);
                }
                $("#cons").text(data.data.cons); 
                $("#consTel").text(data.data.consTel); 
                $("#consMobile").text(data.data.consMobile); 
                $("#consAddr").text(data.data.consAddr); 
            } else {
                window.top.window.showModalAlert(data.msg);
            }
        },
        error: function (XMLHttpRequest, textStatus) {
            if (XMLHttpRequest.status == 500) {
                var result = eval("(" + XMLHttpRequest.responseText + ")");
                alert(result.errorObject.errorText);
            }
        }
    });
}

// SKU信息
function showShiporderSkuInfoTab() {
    if (tblShiporderSkuInfo!=null && tblShiporderSkuInfo != undefined && ""!=tblShiporderSkuInfo){
        tblShiporderSkuInfo.draw();
        return;
    }
    
    var shiporderId = $('#shiporderId', window.parent.document).val();
    
    tblShiporderSkuInfo = $('#tblShiporderSkuInfo').DataTable({
        "bPaginate": false,
        "processing": true,
        "serverSide": true,
        "dom": '<"bottom"rtfli>',
        "searching": false,
        "deferRender": true,
        "info": false,
        "ajax": {
            "url": "/shiporder/getShiporderSkuList.action",
            "data": function ( d ) {
                d.shiporderId = encodeURI(shiporderId);
            }
        },
        "tableTools": {
            "sRowSelect": "bootstrap"
        },
        "columns": [
            {"data": "goodsCode"},
            {"data": "goodsName"},
            {"data": "skuCode"},
            {"data": "skuName"},
            {"data": "qty"},
            {"data": "divideGoodsFee", "render": numberParser},
            {"data": "divideGoodsAmount", "render": numberParser},
            {"data": "goodsSkuCost", "render": numberParser}
        ],
        "columnDefs": [
            {"className": "truncate-text tooltip-column-detail-5", "targets":[0]},
            {"className": "truncate-text tooltip-column-detail", "targets":[1]},
            {"className": "truncate-text tooltip-column-detail-5", "targets":[2]},
            {"className": "truncate-text tooltip-column-detail", "targets":[3]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[4]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[5]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[6]},
            {"className": "truncate-text tooltip-column-detail-5 text-right", "targets":[6]}
        ],
        "order": [[0, "asc"], [2, "asc"]]
    });
    
    // 文字过长截断显示悬浮框
    $("#tblShiporderSkuInfo").tooltip({
        selector: ".tooltip-column-detail-5,.tooltip-column-detail",
        container: "body",
        title: function() {
            if ($(this)[0].scrollWidth > $(this).outerWidth()) {
                return $(this).text();
            }
        }
    });
}